package com.asynctm.android.tm.activity;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.asynctm.android.R;
import com.asynctm.android.common.application.AsyncTMApplication;
import com.asynctm.android.tm.dto.ActivityDto;

/**
 * Dibuja los elementos de la lista.
 * 
 * @author rockdrigo
 */
public class ActivitiesListAdapter extends BaseAdapter {

	private List<ActivityDto> activities;
	private Context context;
	private LayoutInflater layoutInflater;

	/**
	 * 
	 * @param context
	 * @param activities
	 */
	public ActivitiesListAdapter(Context context) {	
		this.context = context;
		this.activities = ((AsyncTMApplication) context).getActivities();
		this.layoutInflater = LayoutInflater.from(this.context);	
	}

	public int getCount() {
		return activities.size();
	}

	public Object getItem(int location) {
		return activities.get(location);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		ActivitiesItemViewHolder activitiesItemViewHolder = null;
		ActivityDto activityDto = activities.get(position);
		View view = convertView;
		if (view == null) {
			view = layoutInflater.inflate(R.layout.activity_list_item, null);
			activitiesItemViewHolder = new ActivitiesItemViewHolder(view);
			view.setTag(activitiesItemViewHolder);
		} else {
			activitiesItemViewHolder = (ActivitiesItemViewHolder) view.getTag();
		}
		
		activitiesItemViewHolder.populate(activityDto);
		return view;
		
	}

}
